package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.google.common.collect.Lists;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 类 名: OrderSettingServiceImpl
 * 描 述:
 * 作 者: 蔡畅
 * 创 建：2019年11月23日
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements  OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        Integer count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if (count > 0) {
            //当前日期已经进行了预约设置,需要进行修改操作
            orderSettingDao.editNumberByOrderDate(orderSetting);
        } else {
            //当前日期已经进行了预约设置,需要进行修改操作
            orderSetting.setReservations(0);
            orderSettingDao.add(orderSetting);
        }

    }

    @Override
    public void setNumberByDateBatch(List<OrderSetting> orderSettings) {
        long start = System.currentTimeMillis();
//        for (OrderSetting orderSetting : orderSettings) {
//            setNumberByDate(orderSetting);
//        }
        //使用多线程导入

        //1、把数据（orderSettings）拆分
        List<List<OrderSetting>> partition = Lists.partition(orderSettings, 20);

        //2、搞一个线程池
        ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);

        for (List<OrderSetting> settings : partition) {
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    for (OrderSetting orderSetting : settings) {
                        editNumberByDate(orderSetting);
                    }
                }
            });
        }

        long end = System.currentTimeMillis();

        System.out.println(end - start);
    }

    /**
     * 根据;日期查询预约设置数据
     *
     * @param date
     * @return
     */
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String start = date + "-01";
        String end = date + "-31";
        Map mapParam = new HashMap();
        mapParam.put("start", start);
        mapParam.put("end", end);
        List<OrderSetting> orderSettings = orderSettingDao.getOrderSettingByMonth(mapParam);
        //要把ordersetting转成 {"date":22,"mouth":10,"number":300,"reservations":300,}
        List<Map> result = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettings) {
            Map map = new HashMap();
            map.put("number", orderSetting.getNumber());
            map.put("reservations", orderSetting.getReservations());
            Date orderDate = orderSetting.getOrderDate();
            map.put("date", orderDate.getDate());
            map.put("mouth", orderDate.getMonth());
            result.add(map);
        }
        return result;
    }
}